<template>
	<el-dialog :title="dialog.title" v-model="dialog.isShowDialog" width="500px">
		<el-form ref="formRef" :model="ruleForm" :rules="rules" label-width="auto">
			<el-row class="mb20">
				<el-col :span="24">
					<el-form-item label="字典名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="请输入字典名称" clearable />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="mb20">
				<el-col :span="24">
					<el-form-item label="字典类型" prop="type">
						<el-input v-model="ruleForm.type" placeholder="请输入字典类型" clearable />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="mb20">
				<el-col :span="24">
					<el-form-item label="状态" prop="status">
						<el-radio-group v-model="ruleForm.status">
							<el-radio :label="1">启用</el-radio>
							<el-radio :label="0">禁用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row class="mb20">
				<el-col :span="24">
					<el-form-item label="备注" prop="remarks">
						<el-input
							type="textarea"
							:rows="2"
							placeholder="请输入备注"
							v-model="ruleForm.remarks">
						</el-input>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<el-button @click="onCancel" size="small">取 消</el-button>
			<el-button type="primary" @click="onSubmit" :loading="dialog.submitLoading" size="small">提 交</el-button>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { nextTick, reactive, ref, toRefs } from 'vue';
import { ElMessage } from 'element-plus';
import { dictionariesApi } from '/@/api/admin/index';

const DictionariesApi = dictionariesApi();
export default {
	setup() {
		let callback: Function = () => {};
		const state = reactive({
			dialog: {
				isShowDialog: false,
				type: '',
				title: '',
				submitLoading: false,
			},
			ruleForm: {
				name: '',
				type: '',
				remarks: '',
				status: 1,
				id: '',
			},
			rules: {
				name: [{ required: true, message: '字典名称不能为空', trigger: ['blur'] }],
				type: [{ required: true, message: '字典类型不能为空', trigger: ['blur'] }],
			},
		});
		const formRef = ref();

		/********** 点击事件 **********/
		// 取消
		const onCancel = () => {
			state.dialog = {
				isShowDialog: false,
				type: '',
				title: '',
				submitLoading: false,
			};
		};
		// 提交
		const onSubmit = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (!valid) return;
				state.dialog.submitLoading = true;
				if (state.dialog.type === 'add') {
					_dictAdd(state.ruleForm);
				} else {
					_dictEdit({
						...state.ruleForm,
						dictionaries_config_id: state.ruleForm.id,
					});
				}
			});
		};

		/********** 方法事件 **********/
		// 打开弹窗
		const openDialog = (data: any, params: any, fn: Function) => {
			state.dialog = {
				isShowDialog: true,
				type: data.type,
				title: data.title,
				submitLoading: false,
			};
			callback = fn;
			nextTick(() => {
				(formRef.value as any)?.resetFields();
				if (data.type === 'edit') {
					state.ruleForm = { ...params};
				}
			});
		};

		/********** 网络事件 **********/
		// 添加字典
		const _dictAdd = (params: object) => {
			DictionariesApi.dictionariesConfigAdd(params)
				.then(() => {
					ElMessage.success('操作成功！');
					onCancel();
					callback();
				})
				.catch(() => (state.dialog.submitLoading = false));
		};
		// 编辑字典
		const _dictEdit = (params: object) => {
			DictionariesApi.dictionariesConfigEdit(params)
				.then(() => {
					ElMessage.success('操作成功！');
					onCancel();
					callback();
				})
				.catch(() => (state.dialog.submitLoading = false));
		};

		return {
			formRef,
			onCancel,
			onSubmit,
			openDialog,
			...toRefs(state),
		};
	},
};
</script>

<style scoped lang="scss">
.menu-data-tree {
	width: 100%;
	border: 1px solid #dcdfe6;
	border-radius: 5px;
	padding: 5px;
}
</style>
